#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <syslog.h>
#endif//_WIN32
#include <string.h>
#include "SilmLogUtil.h"

const char *UtilGetLogTypeString(unsigned int type)
{
    switch(type)
    {
    case SILM_LOG_TYPE_ERROR:
        return "ERROR";
    case SILM_LOG_TYPE_INFO:
        return "INFO";
    case SILM_LOG_TYPE_WARN:
        return "WARN";
    case SILM_LOG_TYPE_DEBUG:
    default:
        return "DEBUG";
    }
}

#ifndef _WIN32
static int InnerGetSysLogPriority(unsigned int type)
{
    switch(type)
    {
    case SILM_LOG_TYPE_ERROR:
        return LOG_ERR;
    case SILM_LOG_TYPE_INFO:
        return LOG_INFO;
    case SILM_LOG_TYPE_WARN:
        return LOG_WARNING;
    case SILM_LOG_TYPE_DEBUG:
    default:
        break;
    }
    return LOG_DEBUG;
}
#endif//_WIN32

static void inner_UtilSysLogEx(unsigned int logType, const char *format, va_list args)
{
#ifdef _WIN32
    char msg[2048];
    unsigned int offset;

    offset = sprintf_s(msg, "[%s]:", UtilGetLogTypeString(logType));
    vsprintf_s(msg+offset, sizeof(msg)-offset, format, args);
    OutputDebugStringA(msg);
#else
    vsyslog(InnerGetSysLogPriority(logType), format, args);
#endif//_WIN32
}

void UtilSysLog(unsigned int logType, const char *format, ...)
{
    va_list args;

    va_start(args, format);
    inner_UtilSysLogEx(logType, format, args);
    va_end(args);
}

void UtilParseFilename(IN const char *pFullFilename,
    INOUT char *pDirBuffer OPTIONAL, IN int dirBufLen OPTIONAL,
    INOUT char *pNameBuffer OPTIONAL, IN int nameBufLen OPTIONAL,
    INOUT char *pExtBuffer OPTIONAL, IN int extBufLen OPTIONAL)
{
    int cpylen;
    const char *pDirSep;
    const char *pExtSep;

    if (NULL != pDirBuffer && dirBufLen > 0)
    {
        *pDirBuffer = '\0';
    }
    if (NULL != pNameBuffer && nameBufLen > 0)
    {
        *pNameBuffer = '\0';
    }
    if (NULL != pExtBuffer && extBufLen > 0)
    {
        *pExtBuffer = '\0';
    }
    if (NULL == pFullFilename || '\0' == *pFullFilename)
    {
        return;
    }
    //find last dir char
#ifdef _WIN32
    pDirSep = strrchr(pFullFilename, '\\');
#else
    pDirSep = strrchr(pFullFilename, '/');
#endif//_WIN32
    if (NULL == pDirSep)
    {
        if (dirBufLen > 1)
        {
            memcpy(pDirBuffer, ".", sizeof("."));
        }
        pDirSep = pFullFilename;
    }
    else
    {
        pDirSep++;
        cpylen = (int)(pDirSep - pFullFilename);
        if(dirBufLen > 1)
        {
            if (cpylen > dirBufLen - 1)
            {
                cpylen = dirBufLen - 1;
            }
            memcpy(pDirBuffer, pFullFilename, cpylen);
            pDirBuffer[cpylen] = '\0';
        }
    }
    pExtSep = strrchr(pFullFilename, '.');
    if (NULL == pExtSep || '\0' == *(pExtSep + 1) || pDirSep >= pExtSep)
    {
        //pExtSep point terminal char
        pExtSep = pFullFilename + strlen(pFullFilename);
    }
    else
    {
        int offset = 0;
        extBufLen--;
        while (extBufLen > 0)
        {
            char ch = *(pExtSep + offset);
            if ('\0' == ch)
            {
                break;
            }
            *(pExtBuffer + offset) = ch;
            offset++;
            extBufLen--;
        }
        *(pExtBuffer + offset) = '\0';
    }
    cpylen = (int)(pExtSep - pDirSep);
    if(nameBufLen > 1)
    {
        if (cpylen > nameBufLen - 1)
        {
            cpylen = nameBufLen - 1;
        }
        memcpy(pNameBuffer, pDirSep, cpylen);
        pNameBuffer[cpylen] = '\0';
    }
}

bool UtilStringBeginWith(IN const char *string, IN const char *substring)
{
    if ('\0' == *string || '\0' == *substring)
    {
        return false;
    }
    return (0 == strncmp(string, substring, strlen(substring)));
}

bool UtilStringEndWith(IN const char *string, IN const char *substring)
{
    int len;
    int sublen;

    if ('\0' == *string || '\0' == *substring)
    {
        return false;
    }
    len = strlen(string);
    sublen = strlen(substring);
    if (sublen > len)
    {
        return false;
    }
    return (0 == strncmp(string + len - sublen, substring, sublen));
}
